package com.carltechnology.mahjonggamecaculation.views.impl;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.carltechnology.mahjonggamecaculation.R;
import com.carltechnology.mahjonggamecaculation.base.MyApplication;
import com.carltechnology.mahjonggamecaculation.mode.impl.GameRuntimeDataMode;
import com.carltechnology.mahjonggamecaculation.presenter.IMahjongRunningGamePresenter;
import com.carltechnology.mahjonggamecaculation.presenter.impl.MahjongPerGameInputPresenter;
import com.carltechnology.mahjonggamecaculation.presenter.impl.MahjongRunningGamePresenter;
import com.carltechnology.mahjonggamecaculation.utils.Utils;
import com.carltechnology.mahjonggamecaculation.views.IMahjongRunningGameView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by WZTCM on 2016/7/1.
 */
public class MahjongRunningGameActivity extends AppCompatActivity implements IMahjongRunningGameView {
    public enum INTENT_CODE{
        REQUEST_RUNNING_CODE(1|1<<4),
        REQUEST_MODIFY_CODE(2|1<<4);
        private int code;
        INTENT_CODE(int code){
            this.code = code;
        }
        public int getCode(){
            return code;
        }
        public int getRequest(){
            return code&0xF0;
        }
        public int getResult(){
            return code&0x0F;
        }
    }
    public static final String PARCEL_NAME = "mode";
    public static final String POS_NAME = "position";
    @InjectView(R.id.mahjonggame_percontent_list)
    RecyclerView recyclerView;
    @InjectView(R.id.mahjongrunning_tool_bar)
    Toolbar toolbar;
    @InjectView(R.id.mahjonggame_runningtotal_bar)
    LinearLayout linearLayout;
    @InjectView(R.id.mahjonggame_runningtotal_account1)
    TextView account1;
    @InjectView(R.id.mahjonggame_runningtotal_account2)
    TextView account2;
    @InjectView(R.id.mahjonggame_runningtotal_account3)
    TextView account3;
    @InjectView(R.id.mahjonggame_runningtotal_account4)
    TextView account4;
    @InjectView(R.id.mahjonggame_runningtotal_cost)
    TextView cost;
    @InjectView(R.id.mahjonggame_runningtotal_perunit)
    TextView perunit;

    private IMahjongRunningGamePresenter presenter;
    private Handler handler = new Handler();
  //  private volatile boolean isRightEnding = false;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mahjonggame_percontent);
        ButterKnife.inject(this);
        setSupportActionBar(toolbar);
        toolbar.setTitle(getTitle());
        assert recyclerView!=null;
        presenter=MahjongRunningGamePresenter.newInstance(this);
        perunit.setText("P:"+MyApplication.getPool().getPerUnit());
        Utils.getInstance().getRuntimeDataHelper().saveRuntimeData();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MahjongPerGameInputPresenter.getInstance(null,-1,-1).doDestroy();
        presenter.onDestroy();
    }

    @Override
    public void ToModifyActivity(int pos, GameRuntimeDataMode mode) {
        Intent intent = new Intent(this,MahjongGamePerInputActivity.class);
        intent.putExtra(POS_NAME,pos);
        intent.putExtra(PARCEL_NAME,mode);
        startActivityForResult(intent,INTENT_CODE.REQUEST_MODIFY_CODE.getRequest());
    }

    @Override
    public RecyclerView getRecycleView() {
        return recyclerView;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public LinearLayout getTotalBar() {
        return linearLayout;
    }

    @Override
    public TextView getAccount1() {
        return account1;
    }

    @Override
    public TextView getAccount2() {
        return account2;
    }

    @Override
    public TextView getAccount3() {
        return account3;
    }

    @Override
    public TextView getAccount4() {
        return account4;
    }

    @Override
    public TextView getCost() {
        return cost;
    }

    @Override
    public Handler getHandler() {
        return handler;
    }

    @OnClick(R.id.mahjonggame_percontent_btn_yes)
    public void onYesBtn(View view){
        Intent intent = new Intent(this,MahjongGamePerInputActivity.class);
        startActivityForResult(intent,INTENT_CODE.REQUEST_RUNNING_CODE.getRequest());
    }
    @OnClick(R.id.mahjonggame_percontent_btn_no)
    public void onNoBtn(View view){
        finish();
        presenter.finishRunningGame();
        Utils.getInstance().getRuntimeDataHelper().clearRuntimeData();

    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        Snackbar.make(getRecycleView(),
                "必须按结束键完成本次编辑",
                Snackbar.LENGTH_SHORT)
                .show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode==INTENT_CODE.REQUEST_RUNNING_CODE.getResult()){
            GameRuntimeDataMode mode = data.getParcelableExtra(PARCEL_NAME);
            presenter.insertMode(mode);
            return ;
        }
        if (resultCode==INTENT_CODE.REQUEST_MODIFY_CODE.getResult()){
            GameRuntimeDataMode mode = data.getParcelableExtra(PARCEL_NAME);
            int pos = data.getIntExtra(POS_NAME,-1);
            presenter.modifyMode(pos,mode.getGameCount(),mode.getCost());
        }
    }
}
